package jee.boot.entity.bpm;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jee.boot.common.annotation.Column;
import jee.boot.common.annotation.Table;
import jee.boot.common.annotation.Transient;
import jee.boot.common.basic.DataEntity;
import jee.boot.common.utils.JsonMapper;
import jee.boot.common.utils.StringUtils;
import jee.boot.entity.auth.Resource;

import java.util.List;
import java.util.Map;

/**
 * 表单定义
 */
@Table(name = "bpm_form_definition")
public class FormDefinition extends DataEntity<FormDefinition> {
    /**
     * 表单类型 字典:form_type
     */
    private String type;
    /**
     * alias
     */
    private String alias;
    /**
     * 名字
     */
    private String name;
    /**
     * 描述
     */
    private String description;

    /**
     * 业务对象
     */
    @Column(data = {"businessObject.id", "businessObject.name", "businessObject.alias", "businessObject.description", "businessObject.relationJson"})
    private BusinessObject businessObject;
    /**
     * 表单设计的json
     */
    private String designJson;
    /**
     * 表单各种配置的json
     */
    private String configJson;
//    /**
//     * 表单设计数据结构,数据结构前端随意定义,后端直接json存储
//     */
//    @Transient
//    private Map<String, Object> design;
    /**
     * 表单设计配置数据,数据结构前端随意定义,后端直接json存储
     */

    private String formStatus;

    private String publish;

    @Transient
    private Map<String, Object> config;

    @Transient
    private Resource resource;

    @Transient
    private List<Resource> resourceButtonList;

    public FormDefinition() {
        super();
    }

    public String getFormStatus() {
        return formStatus;
    }

    public void setFormStatus(String formStatus) {
        this.formStatus = formStatus;
    }

    public List<Resource> getResourceButtonList() {
        return resourceButtonList;
    }

    public void setResourceButtonList(List<Resource> resourceButtonList) {
        this.resourceButtonList = resourceButtonList;
    }

    public Resource getResource() {
        return resource;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }

    public String getPublish() {
        return publish;
    }

    public void setPublish(String publish) {
        this.publish = publish;
    }

    public FormDefinition(String name) {
        super();
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public BusinessObject getBusinessObject() {
        return businessObject;
    }

    public void setBusinessObject(BusinessObject businessObject) {
        this.businessObject = businessObject;
    }

    public String getDesignJson() {
        return designJson;
    }

    public void setDesignJson(String designJson) {
        this.designJson = designJson;
    }

    public String getConfigJson() {
        return configJson;
    }

    public void setConfigJson(String configJson) {
        this.configJson = configJson;
    }

//    @JsonIgnore
//    public Map<String, Object> getDesign() {
//        if (design == null && StringUtils.isNotBlank(designJson)) {
//            design = JsonMapper.fromJsonString(designJson, Map.class);
//        }
//        return design;
//    }
//
//    public void setDesign(Map<String, Object> design) {
//        this.design = design;
//        if (design != null) {
//            designJson = JsonMapper.toJsonString(design);
//        }
//    }

    @JsonIgnore
    public Map<String, Object> getConfig() {
        if (StringUtils.isNotBlank(configJson)) {
            config = JsonMapper.fromJsonString(configJson, Map.class);
        }
        return config;
    }

    public void setConfig(Map<String, Object> config) {
        this.config = config;
        if (config != null) {
            configJson = JsonMapper.toJsonString(config);
        }
    }

    /**
     * 转换之后的表单配置
     *
     * @return
     */
    public FormConfig coverConfig() {
        return JsonMapper.fromJsonString(configJson, FormConfig.class);
    }
}
